<template>
  <el-dialog :title="showTitle" :visible="isShowAdd" @close="btnCancel">
    <el-form :model="formData" :rules="rules" ref="ruleForm" label-width="120px">
      <el-form-item label="部门名称"  prop="name">
        <el-input v-model="formData.name" style="width:80%" placeholder="1-50个字符" />
      </el-form-item>
      <el-form-item label="部门编码"  prop="code">
        <el-input v-model="formData.code" style="width:80%" placeholder="1-50个字符" />
      </el-form-item>
      <el-form-item label="部门负责人"  prop="manager">
        <el-select @focus="getEmployeeSimple" v-model="formData.manager" style="width:80%" placeholder="请选择">
          <el-option
            v-for="item in peoples"
            :key="item.id"
            :label="item.username"
            :value="item.username">
          </el-option>
        </el-select>
      </el-form-item>
      <el-form-item label="部门介绍"  prop="introduce">
        <el-input v-model="formData.introduce" style="width:80%" placeholder="1-300个字符" type="textarea" :rows="3" />
      </el-form-item>
    </el-form>
    <el-row slot="footer" type="flex" justify="center">
      <el-col :span="6">
        <el-button type="primary" size="small" @click="btnOK">确定</el-button>
        <el-button size="small" @click="btnCancel">取消</el-button>
      </el-col>
    </el-row>
  </el-dialog>
</template>

<script>
import { getDepartments, addDepartments, getDepartDetail, updateDepartments } from '@/api/department'
import { getEmployeeSimple } from '@/api/employees'

export default {
  name: 'add-dept',
  props: ['isShowAdd', 'node'],
  data() {
    const validateName = async (rule, value, callback) => {
      const { depts } = await getDepartments()

      let flag = ''

      if(this.formData.id) {
        //                                我需要查找到所有同级部门          把同级部门的我自己移除掉                     比较名字是否相同
        flag = depts.filter(item => item.pid === this.formData.pid && item.id !== this.formData.id ).some(item => value === item.name)
      } else {
        flag = depts.filter(item => item.pid === this.node.id).some(item => item.name === value)
      }

      flag ? callback(new Error(`当前部门已存在${value}`)) : callback()
    }

    const validateCode = async (rule, value, callback) => {
      const { depts } = await getDepartments()

      let flag = ''
      if (this.formData.id) {
        //                    先找到除了我自己所有部门                   比较部门编号是否相同
        flag = depts.filter(item => item.id !== this.formData.id).some(item => item.code === value)
      } else {
        flag = depts.some(item => item.id === value)
      }

      flag ? callback(new Error(`已存在${value}编号`)) : callback()
    }

    return {
      formData: {
        name: '',
        code: '',
        manager: '',
        introduce: ''
      },
      rules: {
        name: [
          { required: true, message: '部门名称不能为空', trigger: 'blur' },
          { min: 1, max: 50, message: '部门名称要求1-50个字符', trigger: 'blur' },
          { validator: validateName, trigger: 'blur' }
        ],
        code: [
          { required: true, message: '部门编码不能为空', trigger: 'blur' },
          { min: 1, max: 50, message: '部门编码要求1-50个字符', trigger: 'blur' },
          { validator: validateCode, trigger: 'blur' }
        ],
        manager: [
          { required: true, message: '部门负责人不能为空', trigger: 'blur' }
        ],
        introduce: [
          { required: true, message: '部门介绍不能为空', trigger: 'blur' },
          { trigger: 'blur', min: 1, max: 300, message: '部门介绍要求1-300个字符' }
        ]
      },
      peoples: []
    }
  },
  methods: {
    async getEmployeeSimple () {
      this.peoples = await getEmployeeSimple()
    },
    btnOK () {
      this.$refs.ruleForm.validate(async isOK => {
        if (!isOK) {
          return false
        }

        if(this.formData.id) {
          await updateDepartments({ ...this.formData })
        } else {
          await addDepartments({ ...this.formData, pid: this.node.id })
        }

        this.$emit('addDepts')
        this.$emit('update:isShowAdd', false)
      })
    },
    btnCancel () {
      this.formData =  {
        name: '',
        code: '',
        manager: '',
        introduce: ''
      }

      this.$refs.ruleForm.resetFields()
      this.$emit('update:isShowAdd', false)
    },
    async getDepartDetail(id) {
      this.formData = await getDepartDetail(id)
    }
  },
  computed: {
    showTitle() {
      return this.formData.id ? '编辑部门' : '添加部门'
    }
  }
}
</script>

<style>

</style>
